<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小红书搜索工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #ff6b6b 0%, #ffa726 100%);
            color: white;
            padding: 20px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 28px;
            font-weight: 600;
        }

        .nav-links {
            display: flex;
            gap: 15px;
        }

        .nav-links a {
            color: white;
            text-decoration: none;
            padding: 8px 16px;
            border-radius: 20px;
            background: rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }

        .nav-links a:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .nav-links a.active {
            background: rgba(255, 255, 255, 0.4);
        }

        .user-info {
            position: relative;
        }

        .user-phone {
            background: rgba(255, 255, 255, 0.2);
            padding: 8px 16px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .user-phone:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            min-width: 120px;
            z-index: 1000;
            display: none;
        }

        .dropdown.show {
            display: block;
        }

        .dropdown-item {
            padding: 12px 16px;
            color: #333;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .dropdown-item:hover {
            background: #f8f9fa;
        }

        .main-content {
            padding: 30px;
        }

        .login-section {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
        }

        .login-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .form-row {
            display: flex;
            gap: 15px;
            align-items: end;
        }

        .form-row .form-group {
            flex: 1;
        }

        label {
            font-weight: 600;
            color: #333;
        }

        input[type="text"], input[type="password"] {
            padding: 12px 16px;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }

        input[type="text"]:focus, input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn:disabled {
            cursor: not-allowed;
            opacity: 0.6 !important;
            transform: none !important;
        }

        .btn-success {
            background: linear-gradient(135deg, #4ecdc4 0%, #44a08d 100%);
            color: white;
        }

        .btn-success:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(78, 205, 196, 0.4);
        }

        .btn-warning {
            background: linear-gradient(135deg, #ffa726 0%, #ff7043 100%);
            color: white;
        }

        .btn-warning:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 167, 38, 0.4);
        }

        .search-section {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
        }

        .search-form {
            display: flex;
            gap: 15px;
            align-items: end;
        }

        .search-form .form-group {
            flex: 1;
        }

        .results-section {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 30px;
        }

        .task-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            border-left: 4px solid #667eea;
        }

        .task-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }

        .task-name {
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }

        .task-status {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
        }

        .status-pending { background: #fff3cd; color: #856404; }
        .status-running { background: #d4edda; color: #155724; }
        .status-completed { background: #d1ecf1; color: #0c5460; }
        .status-failed { background: #f8d7da; color: #721c24; }

        .task-info {
            color: #666;
            font-size: 14px;
            margin-bottom: 5px;
        }

        .task-keywords {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
            margin: 10px 0;
        }

        .keyword-tag {
            background: #e9ecef;
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 12px;
            color: #495057;
        }

        .task-actions {
            display: flex;
            gap: 10px;
            margin-top: 15px;
        }

        .btn-small {
            padding: 6px 12px;
            font-size: 12px;
        }

        .results-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .results-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }

        .login-tabs {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }

        .tab-btn {
            padding: 10px 20px;
            border: 2px solid #667eea;
            background: white;
            color: #667eea;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
        }

        .tab-btn:hover {
            background: #667eea;
            color: white;
        }

        .tab-btn.active {
            background: #667eea;
            color: white;
        }

        .result-card {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
        }

        .result-card:hover {
            transform: translateY(-5px);
        }

        .result-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
            border-radius: 10px;
            margin-bottom: 15px;
        }

        .result-title {
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
        }

        .result-info {
            font-size: 14px;
            color: #666;
            margin-bottom: 5px;
        }

        .checkbox-wrapper {
            margin-top: 15px;
        }

        .checkbox-wrapper input[type="checkbox"] {
            margin-right: 8px;
        }

        .hidden {
            display: none !important;
        }

        .loading {
            text-align: center;
            padding: 20px;
            color: #666;
        }

        .error {
            color: #dc3545;
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 10px;
            border-radius: 5px;
            margin-top: 10px;
        }

        .success {
            color: #155724;
            background: #d4edda;
            border: 1px solid #c3e6cb;
            padding: 10px;
            border-radius: 5px;
            margin-top: 10px;
        }

        @media (max-width: 768px) {
            .form-row {
                flex-direction: column;
            }
            
            .search-form {
                flex-direction: column;
            }
            
            .results-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>小红书搜索工具</h1>
            <div class="nav-links">
                <a href="index.html" class="active">首页</a>
                <a href="task.html">任务管理</a>
                <a href="result.html">执行结果</a>
            </div>
            <div class="user-info">
                <div class="user-phone" id="userPhone" onclick="toggleDropdown()">
                    未登录
                </div>
                <div class="dropdown" id="dropdown">
                    <div class="dropdown-item" onclick="reLogin()">重新登录</div>
                </div>
            </div>
        </div>

        <div class="main-content">
            <!-- 登录区域 -->
            <div class="login-section" id="loginSection">
                <h2 style="margin-bottom: 20px;">用户登录</h2>
                
                <!-- 登录方式切换 -->
                <div class="login-tabs" style="margin-bottom: 20px;">
                    <button class="tab-btn active" onclick="switchLoginMode('verify')">验证码登录</button>
                    <button class="tab-btn" onclick="switchLoginMode('direct')">直接登录</button>
                </div>
                
                <!-- 验证码登录表单 -->
                <div class="login-form" id="verifyLoginForm">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="phoneNumber">手机号</label>
                            <input type="text" id="phoneNumber" placeholder="请输入手机号">
                        </div>
                        <button class="btn btn-primary" onclick="inputPhone()">发送验证码</button>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="verifyCode">验证码</label>
                            <input type="text" id="verifyCode" placeholder="请输入验证码">
                        </div>
                        <button class="btn btn-success" onclick="verifyLogin()">登录</button>
                    </div>
                </div>
                
                <!-- 直接登录表单 -->
                <div class="login-form hidden" id="directLoginForm">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="directPhoneNumber">手机号</label>
                            <input type="text" id="directPhoneNumber" placeholder="请输入手机号">
                        </div>
                        <button class="btn btn-success" onclick="directLogin()">直接登录</button>
                    </div>
                </div>
                
                <div id="loginMessage"></div>
            </div>

            <!-- 登录成功区域 -->
            <div class="search-section hidden" id="searchSection">
                <h2 style="margin-bottom: 20px;">登录成功</h2>
                
                <div style="background: white; padding: 20px; border-radius: 10px; text-align: center;">
                    <h3 style="margin-bottom: 15px;">欢迎使用小红书搜索工具</h3>
                    <p style="color: #666; margin-bottom: 20px;">您已成功登录，可以使用以下功能：</p>
                    <div style="display: flex; gap: 20px; justify-content: center;">
                        <a href="task.html" class="btn btn-primary">任务管理</a>
                        <a href="result.html" class="btn btn-success">查看结果</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        let currentSearchResults = [];
        
        // 页面加载时检查登录状态
        window.onload = function() {
            checkLoginStatus();
        };

        // 检查登录状态
        function checkLoginStatus() {
            const phoneNumber = localStorage.getItem('phoneNumber');
            if (phoneNumber) {
                showLoggedInState(phoneNumber);
            }
        }

        // 显示已登录状态
        function showLoggedInState(phoneNumber) {
            document.getElementById('userPhone').textContent = phoneNumber;
            document.getElementById('loginSection').classList.add('hidden');
            document.getElementById('searchSection').classList.remove('hidden');
        }

        // 显示未登录状态
        function showLoggedOutState() {
            document.getElementById('userPhone').textContent = '未登录';
            document.getElementById('loginSection').classList.remove('hidden');
            document.getElementById('searchSection').classList.add('hidden');
            document.getElementById('resultsSection').classList.add('hidden');
            localStorage.removeItem('phoneNumber');
        }

        // 切换下拉菜单
        function toggleDropdown() {
            const dropdown = document.getElementById('dropdown');
            dropdown.classList.toggle('show');
        }

        // 切换登录模式
        function switchLoginMode(mode) {
            const verifyForm = document.getElementById('verifyLoginForm');
            const directForm = document.getElementById('directLoginForm');
            const tabBtns = document.querySelectorAll('.tab-btn');
            
            // 移除所有按钮的active类
            tabBtns.forEach(btn => btn.classList.remove('active'));
            
            if (mode === 'verify') {
                verifyForm.classList.remove('hidden');
                directForm.classList.add('hidden');
                tabBtns[0].classList.add('active');
            } else {
                verifyForm.classList.add('hidden');
                directForm.classList.remove('hidden');
                tabBtns[1].classList.add('active');
            }
            
            // 清空消息
            document.getElementById('loginMessage').innerHTML = '';
        }

        // 重新登录
        function reLogin() {
            showLoggedOutState();
            document.getElementById('dropdown').classList.remove('show');
        }

        // 点击其他地方关闭下拉菜单
        window.onclick = function(event) {
            if (!event.target.matches('.user-phone')) {
                const dropdown = document.getElementById('dropdown');
                if (dropdown.classList.contains('show')) {
                    dropdown.classList.remove('show');
                }
            }
        };

        // 输入手机号
        async function inputPhone() {
            const phoneNumber = document.getElementById('phoneNumber').value;
            const sendButton = event.target;
            
            if (!phoneNumber) {
                showMessage('loginMessage', '请输入手机号', 'error');
                return;
            }

            // 设置加载状态
            sendButton.disabled = true;
            sendButton.textContent = '发送中...';
            sendButton.style.opacity = '0.6';

            try {
                const response = await fetch('/xhs/api/login/input-phone', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        phoneNumber: phoneNumber
                    })
                });

                const data = await response.json();
                
                if (data.code === 200) {
                    showMessage('loginMessage', '验证码已发送，请输入验证码', 'success');
                } else {
                    showMessage('loginMessage', data.message || '发送验证码失败', 'error');
                }
            } catch (error) {
                showMessage('loginMessage', '网络错误，请重试', 'error');
                console.error('Error:', error);
            } finally {
                // 恢复按钮状态
                sendButton.disabled = false;
                sendButton.textContent = '发送验证码';
                sendButton.style.opacity = '1';
            }
        }

        // 验证登录
        async function verifyLogin() {
            const phoneNumber = document.getElementById('phoneNumber').value;
            const verifyCode = document.getElementById('verifyCode').value;
            const loginButton = event.target;
            
            if (!phoneNumber || !verifyCode) {
                showMessage('loginMessage', '请输入完整信息', 'error');
                return;
            }

            // 设置加载状态
            loginButton.disabled = true;
            loginButton.textContent = '登录中...';
            loginButton.style.opacity = '0.6';

            try {
                const response = await fetch('/xhs/api/login/verify-login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        phoneNumber: phoneNumber,
                        verifyCode: verifyCode
                    })
                });

                const data = await response.json();
                
                if (data.code === 200) {
                    localStorage.setItem('phoneNumber', phoneNumber);
                    showLoggedInState(phoneNumber);
                    showMessage('loginMessage', '登录成功', 'success');
                } else {
                    showMessage('loginMessage', data.message || '登录失败', 'error');
                }
            } catch (error) {
                showMessage('loginMessage', '网络错误，请重试', 'error');
                console.error('Error:', error);
            } finally {
                // 恢复按钮状态
                loginButton.disabled = false;
                loginButton.textContent = '登录';
                loginButton.style.opacity = '1';
            }
        }

        // 直接登录
        async function directLogin() {
            const phoneNumber = document.getElementById('directPhoneNumber').value;
            const loginButton = event.target;
            
            if (!phoneNumber) {
                showMessage('loginMessage', '请输入手机号', 'error');
                return;
            }

            // 设置加载状态
            loginButton.disabled = true;
            loginButton.textContent = '登录中...';
            loginButton.style.opacity = '0.6';

            try {
                const response = await fetch('/xhs/api/login/direct-login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        phoneNumber: phoneNumber
                    })
                });

                const data = await response.json();
                
                if (data.code === 200) {
                    showMessage('loginMessage', '登录成功', 'success');
                    
                    // 保存登录信息
                    localStorage.setItem('phoneNumber', phoneNumber);
                    showLoggedInState(phoneNumber);
                } else {
                    showMessage('loginMessage', data.message || '登录失败', 'error');
                }
            } catch (error) {
                showMessage('loginMessage', '网络错误，请重试', 'error');
                console.error('Error:', error);
            } finally {
                // 恢复按钮状态
                loginButton.disabled = false;
                loginButton.textContent = '直接登录';
                loginButton.style.opacity = '1';
            }
        }





        // 显示消息
        function showMessage(elementId, message, type) {
            const element = document.getElementById(elementId);
            element.innerHTML = `<div class="${type}">${message}</div>`;
            
            if (type === 'success') {
                setTimeout(() => {
                    element.innerHTML = '';
                }, 3000);
            }
        }
    </script>
</body>
</html> 